package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.ResignationApplication;
import com.amsp.oa.oademo.service.ResignationApplicationService;
import com.amsp.oa.oademo.vo.ResignationApplicationVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职员/员工离职申请表Controller
 */
@Api(tags = "职员/员工离职申请表接口")
@RestController
@RequestMapping("/api/resignation-application")
public class ResignationApplicationController {

    @Autowired
    private ResignationApplicationService resignationApplicationService;

    @ApiOperation("获取离职申请表列表")
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getList(
            @ApiParam(value = "当前页码", required = true) @RequestParam(defaultValue = "1") Integer current,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(defaultValue = "10") Integer size,
            @ApiParam(value = "姓名") @RequestParam(required = false) String name,
            @ApiParam(value = "部门") @RequestParam(required = false) String department) {

        Page<ResignationApplication> page = new Page<>(current, size);
        Page<ResignationApplicationVO> result = resignationApplicationService.getList(page, name, department);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取列表成功");
        response.put("data", result);

        return ResponseEntity.ok(response);
    }

    @ApiOperation("获取离职申请表详情")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDetail(
            @ApiParam(value = "离职申请表ID", required = true) @PathVariable Long id) {

        ResignationApplicationVO vo = resignationApplicationService.getDetail(id);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取详情成功");
        response.put("data", vo);

        return ResponseEntity.ok(response);
    }

    @ApiOperation("保存或更新离职申请表")
    @PostMapping("/save")
    public ResponseEntity<Map<String, Object>> save(
            @ApiParam(value = "离职申请表数据", required = true) @RequestBody ResignationApplicationVO vo) {

        boolean result = resignationApplicationService.saveOrUpdate(vo);

        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "保存成功");
        } else {
            response.put("code", 500);
            response.put("message", "保存失败");
        }

        return ResponseEntity.ok(response);
    }

    @ApiOperation("批量保存离职申请表")
    @PostMapping("/batch-save")
    public ResponseEntity<Map<String, Object>> batchSave(
            @ApiParam(value = "离职申请表数据列表", required = true) @RequestBody List<ResignationApplicationVO> voList) {

        boolean result = resignationApplicationService.batchSave(voList);

        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "批量保存成功");
        } else {
            response.put("code", 500);
            response.put("message", "批量保存失败");
        }

        return ResponseEntity.ok(response);
    }

    @ApiOperation("删除离职申请表")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(
            @ApiParam(value = "离职申请表ID", required = true) @PathVariable Long id) {

        boolean result = resignationApplicationService.delete(id);

        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "删除成功");
        } else {
            response.put("code", 500);
            response.put("message", "删除失败");
        }

        return ResponseEntity.ok(response);
    }

    @ApiOperation("批量删除离职申请表")
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> batchDelete(
            @ApiParam(value = "离职申请表ID列表", required = true) @RequestBody List<Long> ids) {

        boolean result = resignationApplicationService.batchDelete(ids);

        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "批量删除成功");
        } else {
            response.put("code", 500);
            response.put("message", "批量删除失败");
        }

        return ResponseEntity.ok(response);
    }
}